class Ruiqu::StoresController < Ruiqu::BaseController
  before_action :get_store, only: [:show, :edit, :update]
  # def new
  #   @store = Store.new(store_basic_info: StoreBasicInfo.new, contact_info: ContactInfo.new)
  # end

  def create
    @store = Store.new(store_params)
    if @store.save
      redirect_to new_ruiqu_store_contact_info_path, notice: "Store info was successful!"
    else
      redirect_to new_ruiqu_store_path, notice: "I'm sorry that fails"
    end
  end

  def show

  end

  def edit

  end

  def update
    @store.update!(store_params.except(:img))
    if store_params[:img].present?
      img ||= store_params[:img]
      status = QiniuService.call(img.path, img.original_filename, @store)
      if status.success?
        @store.update!(img: status.notice["key"])
      end
    end
    redirect_to ruiqu_store_path
  end

  private
  def store_params
    params.require(:store).permit(
      :name,
      :img,
      store_basic_info_attributes: [
        :legal_person,
        :address,
        :register_money,
        :company_type,
        :set_from,
        :main_industry
      ]
    )
  end

  def get_store
    @store = current_store
  end
end
